package org.create.builder;

import org.apache.commons.lang3.ArrayUtils;
import org.create.bean.FieldInfo;
import org.create.bean.TableInfo;
import org.create.utils.DateTool;
import org.create.utils.IoTool;
import org.create.utils.LogTool;

import java.io.*;
import java.util.Date;
import java.util.List;

import static org.create.bean.Constants.*;


public class BuildComment {
    private static BufferedWriter bw;

    private static void buildPackageComment(TableInfo tableInfo) throws IOException {
        bw.write("package " + PACKAGE_BASE + "." + PACKAGE_PO + ";");
        appendLine(2);
        if (tableInfo.getFieldList().stream().anyMatch(e -> e.getFieldName().equals(FIELD_IGNORE))) {
            bw.write("import com.fasterxml.jackson.annotation.JsonIgnore;");
            appendLine();
        }
        if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
            bw.write("import com.fasterxml.jackson.annotation.JsonFormat;");
            appendLine();
            bw.write("import org.springframework.format.annotation.DateTimeFormat;");
            appendLine(2);
        }
        bw.write("import java.io.Serializable;");
        appendLine();
        if (tableInfo.getHaveBigDecimal()) {
            bw.write("import java.math.BigDecimal;");
            appendLine();
        }
        if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
            bw.write("import java.util.Date;");
        }
        appendLine(3);
    }

    private static void buildDescriptionComment(TableInfo tableInfo) throws IOException {
        bw.write("/**");
        appendLine();
        bw.write(" * Description " + tableInfo.getComment());
        appendLine();
        bw.write(" * date " + DateTool.DateFormat(new Date(), YMD_HMS));
        appendLine();
        bw.write(" */");
        appendLine();
    }

    private static void buildClassComment(TableInfo tableInfo) throws IOException {
        bw.write("public class " + tableInfo.getBeanName() + " implements Serializable {");
        appendLine();
        buildFieldComment(tableInfo.getFieldList());
        appendLine();
        buildMethodDescriptionComment(tableInfo.getFieldList());
        buildToString(tableInfo);
        bw.write("}");
    }

    private static void buildFieldComment(List<FieldInfo> fieldInfoList) throws IOException {
        for (FieldInfo fieldInfo : fieldInfoList) {
            buildFieldDescriptionComment(fieldInfo);
            buildAnnotationComment(fieldInfo);
            bw.write("    private " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";");
            appendLine();
        }
    }

    private static void buildAnnotationComment(FieldInfo fieldInfo) throws IOException {
        if (fieldInfo.getFieldName().equals(FIELD_IGNORE)) {
            bw.write("    @JsonIgnore");
            appendLine();
        }
        String sqlType = fieldInfo.getSqlType();
        sqlType = sqlType.contains("(") ? sqlType.split("\\(")[0] : sqlType;
        if (ArrayUtils.contains(SQL_DATE_TIME_TYPES, sqlType)) {
            bw.write("    @JsonFormat(pattern = \"" + YMD_HMS + "\", timezone = \"" + PATTERN_TIMEZONE + "\")");
            appendLine();
            bw.write("    @DateTimeFormat(pattern = \"" + YMD_HMS + "\")");
            appendLine();
        }
        if (ArrayUtils.contains(SQL_DATE_TYPES, sqlType)) {
            bw.write("    @JsonFormat(pattern = \"" + YMD + "\", timezone = \"" + PATTERN_TIMEZONE + "\")");
            appendLine();
            bw.write("    @DateTimeFormat(pattern = \"" + YMD + "\")");
            appendLine();
        }
    }

    private static void buildFieldDescriptionComment(FieldInfo fieldInfo) throws IOException {
        bw.write("    /**");
        appendLine();
        bw.write("     * " + fieldInfo.getComment());
        appendLine();
        bw.write("     */");
        appendLine();
    }

    private static void buildMethodDescriptionComment(List<FieldInfo> fieldInfoList) throws IOException {
        for (FieldInfo fieldInfo : fieldInfoList) {
            bw.write("    public " + fieldInfo.getJavaType() + " get");
            bw.write(fieldInfo.getPropertyName().substring(0, 1).toUpperCase());
            bw.write(fieldInfo.getPropertyName().length() > 1 ? fieldInfo.getPropertyName().substring(1) : "");
            bw.write("() {");
            appendLine();
            bw.write("        return " + fieldInfo.getPropertyName() + ";");
            appendLine();
            bw.write("    }");
            appendLine(2);
            bw.write("    public void set");
            bw.write(fieldInfo.getPropertyName().substring(0, 1).toUpperCase());
            bw.write(fieldInfo.getPropertyName().length() > 1 ? fieldInfo.getPropertyName().substring(1) : "");
            bw.write("(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {");
            appendLine();
            bw.write("        this." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";");
            appendLine();
            bw.write("    }");
            appendLine(2);
        }
    }

    public static void buildToString(TableInfo tableInfo) throws IOException {
        boolean firstLoop = true;
        bw.write("    @Override");
        appendLine();
        bw.write("    public String toString() {");
        appendLine();
        bw.write("        return \"" + tableInfo.getBeanName() + "{\" +");
        appendLine();
        for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
            if (firstLoop) {
                bw.write("                \"" + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +");
                appendLine();
                firstLoop = false;
            } else {
                bw.write("                \", " + fieldInfo.getPropertyName() + "=\" + " + fieldInfo.getPropertyName() + " +");
                appendLine();
            }
        }
        bw.write("                '}';");
        appendLine();
        bw.write("    }");
        appendLine();
    }

    private static void appendLine() throws IOException {
        appendLine(1);
    }

    private static void appendLine(int loop) throws IOException {
        if (loop == 1) {
            bw.newLine();
        } else {
            for (int i = 0; i < loop; i++) {
                bw.newLine();
            }
        }
    }

    public static void run(List<TableInfo> tableInfoList) {
        for (TableInfo tableInfo : tableInfoList) {
            File file = new File(PATH_PO + tableInfo.getBeanName() + JAVA_SUFFIX);
            File dir = file.getParentFile();
            boolean d = dir.mkdirs();
            LogTool.printLog(d, "新建目录", dir);
            try {
                boolean r = file.createNewFile();
                LogTool.printLog(r, "新建文件", file);
                bw = IoTool.createBw(file);
                buildPackageComment(tableInfo);
                buildDescriptionComment(tableInfo);
                buildClassComment(tableInfo);
            } catch (Exception e) {
                LogTool.printError(e);
            } finally {
                IoTool.close();
            }
        }
    }
}
